package com.why.whyoj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.why.whyoj.common.BaseResponse;
import com.why.whyoj.common.ErrorCode;
import com.why.whyoj.common.ResultUtils;
import com.why.whyoj.exception.BusinessException;
import com.why.whyoj.exception.ThrowUtils;
import com.why.whyoj.model.dto.question.QuestionQueryRequest;
import com.why.whyoj.model.dto.questionsubmit.QuestionSubmitAddRequest;
import com.why.whyoj.model.dto.questionsubmit.QuestionSubmitQueryRequest;
import com.why.whyoj.model.entity.Question;
import com.why.whyoj.model.entity.QuestionSubmit;
import com.why.whyoj.model.entity.User;
import com.why.whyoj.model.vo.QuestionSubmitQueryVO;
import com.why.whyoj.model.vo.QuestionVO;
import com.why.whyoj.service.QuestionSubmitService;
import com.why.whyoj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 题目提交接口
 *
 *
 *
 */
@RestController
//@RequestMapping("/question_submit")
@Slf4j
@Deprecated
public class QuestionSubmitController {

    @Resource
    private QuestionSubmitService questionSubmitService;

    @Resource
    private UserService userService;

    /**
     * 提交题目
     *
     * @param questionSubmitAddRequest
     * @param request
     * @return resultNum 本次点赞变化数
     */
    @PostMapping("/do_submit")
    public BaseResponse<Long> doQuestionSubmit(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest,
            HttpServletRequest request) {
        if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        final User loginUser = userService.getLoginUser(request);
        long result = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);
        return ResultUtils.success(result);
    }

    @PostMapping("/get_submit")
    public BaseResponse<QuestionSubmitQueryVO> getQuestionSubmitById(
            @RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest,
            HttpServletRequest request){

        long questionSubmitId;
        if (questionSubmitQueryRequest == null || (questionSubmitId = questionSubmitQueryRequest.getQuestionSubmitId()) <= 0) return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        User user = userService.getLoginUser(request);
        return ResultUtils.success(questionSubmitService.getQuestionSubmitById(questionSubmitId, user));
    }

    @PostMapping("/get_submit_by_page")
    public BaseResponse<Page<QuestionSubmitQueryVO>> listQuestionSubmitQueryVOByPage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryRequest,
                                                               HttpServletRequest request) {
        long current = questionSubmitQueryRequest.getCurrent();
        long size = questionSubmitQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(current, size),
                questionSubmitService.getQueryWrapper(questionSubmitQueryRequest));
        User user = userService.getLoginUser(request);
        Page<QuestionSubmitQueryVO> fianlPage = new Page<>();
        fianlPage.setRecords(questionSubmitService.questionSubmitListToVoList(questionSubmitPage.getRecords(), user));
        return ResultUtils.success(fianlPage);
    }
}
